package com.culture.config.service.impl;

import javax.annotation.Resource;

import com.culture.config.common.util.BeanUtil;
import com.culture.config.dao.entity.User;
import com.culture.config.dao.mapper.UserMapper;
import com.culture.config.service.UserService;
import com.culture.config.model.po.*;
import com.culture.config.model.vo.*;
import com.culture.config.wrapper.UserWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class UserServiceImpl implements UserService{
    @Resource
    private UserMapper userMapper;

    @Override
    public Page<UserVO> page(UserPageParamPO param) {
        log.info("page:{}",param);
        PageMethod.startPage(param.getPageNum(),param.getPageSize());
		Page<User> userList = this.userMapper.page(UserWrapper.wrapUserQuery(param));      
		return BeanUtil.copyPageProperties(userList, UserVO.class);
    }

    @Override
    @Transactional
    public void add(UserAddPO userAddPO) {
        log.info("add:{}",userAddPO);
        User newUser =  UserWrapper.wrapUserAdd(userAddPO);
        int ret = this.userMapper.add(newUser);
        log.info("Add User,ret:{}",ret,newUser);
    }

    @Override
    @Transactional
    public void update(UserUpdatePO userUpdatePO) {
        log.info("update:{}",userUpdatePO);
        User updateUser =  UserWrapper.wrapUserUpdate(userUpdatePO);
        int ret =  this.userMapper.update(updateUser);
        log.info("Update User,ret:{}",ret, updateUser);
    }

    @Override
    public void updatePassword(UserPasswordUpdatePO userPasswordUpdatePO) {
        log.info("updatePassword:{}",userPasswordUpdatePO.getId());
        User updateUser =  UserWrapper.wrapUserPwdUpdate(userPasswordUpdatePO);
        int ret = this.userMapper.updatePassword(updateUser);
        log.info("Update User,ret:{}",ret, updateUser);
    }
    
}
